package com.demo.util;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Arrays;

public class NumberUtil {

     public static int PRECISION = 24;//代表生成数字有几位，不是小数位

    public static int SCALE = 6;//小数位

    public static MathContext DEFAULT_CONTEXT = new MathContext(PRECISION, RoundingMode.HALF_UP);

    public static MathContext UP_CONEXT = new MathContext(PRECISION, RoundingMode.UP);//绝对值向上取整

    public static MathContext DOWN_CONEXT = new MathContext(PRECISION, RoundingMode.DOWN);//绝对值向下取整

    public static DecimalFormat FORMATER = new DecimalFormat("#.######");

    public static String formatValue(String amount) {
        return formatValue(amount, 1000000);
    }

    public static String formatValue(String amount, long size) {
        BigDecimal value = new BigDecimal(amount);
        return FORMATER.format(value.divide(BigDecimal.valueOf(size), DEFAULT_CONTEXT));
    }

    public static int compaire(long x, long y) {
        if (x < y) {
            return -1;
        }
        return (x == y) ? 0 : 1;
    }

    /**
     * 带百分号字符串转成BigDecimal
     * 2位小数
     * 
     * @param percentage
     * @return
     */
    public static BigDecimal percentageToBigDecimal(String percentage) {
        BigDecimal bigDecimal = new BigDecimal(percentage);
        return bigDecimal.divide(new BigDecimal("100"), DEFAULT_CONTEXT).setScale(2, RoundingMode.HALF_UP);
    }
    public static String formatPercent(String percentage) {
        NumberFormat percent = NumberFormat.getPercentInstance();  //建立百分比格式化引用
        percent.setMaximumFractionDigits(2); //百分比小数点最多3位
        BigDecimal bigDecimal = new BigDecimal(percentage);
        return percent.format(bigDecimal);
    }

    /**
     * 安全减法
     * 
     * @param first
     * @param second
     * @return
     */
    public static BigDecimal subtract(BigDecimal first, BigDecimal second) {
        return subtract(first, second, DEFAULT_CONTEXT);
    }

    public static BigDecimal subtract(BigDecimal first, BigDecimal second, MathContext mc) {
        if (second == null) {
            return first;
        }
        if (first == null) {
            return BigDecimal.ZERO;
        }
        return first.subtract(second, mc).setScale(SCALE, RoundingMode.HALF_UP);
    }

    /**
     * 安全减法
     * 
     * @param first
     * @param second
     * @return
     */
    public static BigDecimal add(BigDecimal first, BigDecimal second) {
        return add(first, second, DEFAULT_CONTEXT);
    }

    public static BigDecimal add(BigDecimal first, BigDecimal second, MathContext mc) {
        if (second == null) {
            return first;
        }
        if (first == null) {
            return second;
        }
        return first.add(second, mc).setScale(SCALE, RoundingMode.HALF_UP);
    }

    /**
     * 安全除法
     * 
     * @param first
     * @param second
     * @return
     */
    public static BigDecimal divid(BigDecimal first, BigDecimal second) {
        return divid(first, second, DEFAULT_CONTEXT);
    }

    public static BigDecimal divid(BigDecimal first, BigDecimal second, MathContext mc) {
        if (second == null) {
            return BigDecimal.ZERO;
        }
        if (first == null || first.equals(BigDecimal.ZERO) || second.equals(BigDecimal.ZERO)) {
            return BigDecimal.ZERO;
        }
        return first.divide(second, mc).setScale(SCALE, RoundingMode.HALF_UP);
    }

    public static BigDecimal multiply(BigDecimal first, BigDecimal second) {
        return multiply(first, second, DEFAULT_CONTEXT);
    }

    public static BigDecimal multiply(BigDecimal first, BigDecimal second, MathContext mc) {
        if (second == null || first == null) {
            return BigDecimal.ZERO;
        }
        return first.multiply(second, mc).setScale(SCALE, RoundingMode.HALF_UP);
    }

    public static int getValue(BigDecimal first, RoundingMode model) {
        return first.setScale(0, model).intValue();
    }

    public static int getRoundUpValue(BigDecimal first) {
        return getValue(first, RoundingMode.UP);
    }

    public static int getRoundDwonValue(BigDecimal first) {
        return getValue(first, RoundingMode.DOWN);
    }

    /**
     * 检查是否有小数
     * 
     * @param value
     * @return
     */
    public static boolean hasDecimal(BigDecimal value) {
        return value.subtract(new BigDecimal(value.longValue())).compareTo(BigDecimal.ZERO) != 0;
        //return value.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO) != 0;
    }

    public static void main(String[] args) {
        BigDecimal a = new BigDecimal("3.0E+1");
        BigDecimal d = new BigDecimal("10.0");
        System.out.println(a.intValue());
        BigDecimal b = new BigDecimal("0.300000");
        BigDecimal c = a.multiply(b, DEFAULT_CONTEXT);
        System.out.println(c.toString());
        c = divid(d,b);
        System.out.println(c.toString());
        System.out.println(" ".equals(" "));
        System.out.println(Arrays.toString(" ".getBytes()));
        System.out.println(Arrays.toString(" ".getBytes()));
    }

}
